package org.ciallo.simplemusicplayer.activity;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import org.ciallo.simplemusicplayer.R;
import timber.log.Timber;

import java.util.Locale;

/**
 * @author Jukomu
 * @Description: "关于"页面，显示应用信息
 * @Package: org.ciallo.simplemusicplayer.activity
 * @Project: SimpleMusicPlayer
 * @name: AboutActivity
 * @Date: 2025/6/13-04:30
 * @Filename: AboutActivity
 */
public class AboutActivity extends AppCompatActivity {

    private TextView tvVersion;
    private TextView tvEmail;
    private TextView tvGithub;

    private static final String DEVELOPER_EMAIL = "jukomu@qq.com"; // 替换为你的邮箱
    private static final String DEVELOPER_GITHUB_URL = "https://github.com/Jukomu"; // 替换为你的GitHub主页链接

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);

        Toolbar toolbar = findViewById(R.id.toolbar_about_activity);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        }

        tvVersion = findViewById(R.id.textView_about_version);
        tvEmail = findViewById(R.id.textView_developer_email);
        tvGithub = findViewById(R.id.textView_developer_github);

        setAppVersion(tvVersion);
        setupContactInfo();
    }

    /**
     * 获取并设置应用版本信息。
     * @param textView 用于显示版本信息的TextView。
     */
    private void setAppVersion(TextView textView) {
        try {
            PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
            String versionName = pInfo.versionName;
            int versionCode = pInfo.versionCode;
            textView.setText(String.format(Locale.getDefault(), "版本 %s (%d)", versionName, versionCode));
        } catch (PackageManager.NameNotFoundException e) {
            Timber.e(e, "Could not get package info to set app version.");
            textView.setText("版本未知");
        }
    }

    /**
     * 设置联系信息和点击事件。
     */
    private void setupContactInfo() {
        // 设置显示的文本
        tvEmail.setText(DEVELOPER_EMAIL);
        // 从URL中提取用户名/仓库名作为显示文本
        tvGithub.setText(DEVELOPER_GITHUB_URL.replaceFirst("https?://", ""));


        // 设置邮箱点击事件
        tvEmail.setOnClickListener(v -> {
            Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
            emailIntent.setData(Uri.parse("mailto:" + DEVELOPER_EMAIL));
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, "关于 SimpleMusicPlayer 应用的反馈");
            try {
                startActivity(emailIntent);
            } catch (ActivityNotFoundException e) {
                Timber.e(e, "No email client found.");
                Toast.makeText(this, "未找到邮件应用", Toast.LENGTH_SHORT).show();
            }
        });

        // 设置GitHub链接点击事件
        tvGithub.setOnClickListener(v -> {
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(DEVELOPER_GITHUB_URL));
            try {
                startActivity(browserIntent);
            } catch (ActivityNotFoundException e) {
                Timber.e(e, "No web browser found.");
                Toast.makeText(this, "未找到网页浏览器", Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            onBackPressed();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
